15. CODE: Write FindNeighbors

Write FindNeighbors

In this exercise, modify route_model.h and route_model.cpp to add a RouteModel::Node method FindNeighbors. The goal of FindNeighbors is to populate the neighbors vector of the current Node object (the vector this->neighbors).

Each road that the current node is a part of can be thought of as a possible direction to travel, much like the up, down, left, right directions from the first A* search project. This means that you will want to find the closest neighbor from each road that the current node this belongs to. You can get each road that the current node belongs to using the node_to_road hash table as follows:

parent_model->node_to_road[this->index]

Once you have a road, you can get a vector containing all other node indices on that road with a similar construction to what you have seen before:

parent_model->Ways()[road->way].nodes

You can then use the FindNeighbor method with that vector of node indices to find a pointer to the closest node.

Since FindNeighbors operates on the current Node and modifies the Node data, it does not need any arguments and can have return type void.

## To complete this exercise:

  1. Add a public FindNeighbors declaration to the RouteModel::Node class in route_model.h. This method will be called from route_planner.cpp, so the method needs to be public. FindNeighbors should take no arguments and have void return type.
  2. In route_model.cpp define the FindNeighbors method.
  3. With the FindNeighbors method, for each road reference &road in the vector parent_model->node_to_road[this->index], FindNeighbors should use the FindNeighbor method to create a pointer of RouteModel::Node* type.
  4. If that pointer is not a nullptr, push the pointer to the back of this->neighbors.

Workspace

This section contains either a workspace (it can be a Jupyter Notebook workspace or an online code editor work space, etc.) and it cannot be automatically downloaded to be generated here. Please access the classroom with your account and manually download the workspace to your local machine. Note that for some courses, Udacity upload the workspace files onto https://github.com/udacity, so you may be able to download them there.

Workspace Information:

  • Default file path:
  • Workspace type: react
  • Opened files (when workspace is loaded): n/a
  • userCode:

    export CXX=g++-7
    export CXXFLAGS=-std=c++17
    cmake_tests() {
    /usr/local/bin/cmake -DTESTING="FindNeighbors" "$1"
    }
    export -f cmake_tests

Solution

Find Neighbors 2